package cn.sfrog.project.biz.admin.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.sfrog.common.constant.Constants;
import cn.sfrog.common.utils.SecurityUtils;
import cn.sfrog.common.utils.StringUtils;
import cn.sfrog.project.biz.admin.domain.BizCombo;
import cn.sfrog.project.biz.admin.domain.BizService;
import cn.sfrog.project.biz.admin.domain.BizServiceUser;
import cn.sfrog.project.biz.admin.domain.vo.BizComboFormVO;
import cn.sfrog.project.biz.admin.domain.vo.BizComboQueryVO;
import cn.sfrog.project.biz.admin.domain.vo.BizComboVO;
import cn.sfrog.project.biz.admin.mapper.BizComboMapper;
import cn.sfrog.project.biz.admin.service.IBizComboService;
import cn.sfrog.project.biz.web.vo.combo.AppChangeStatusVO;
import cn.sfrog.project.biz.web.vo.combo.AppComboDetailResultVO;
import cn.sfrog.project.biz.web.vo.combo.AppComboSubServiceVO;
import cn.sfrog.project.biz.web.vo.service.AppServiceDetailResultVO;
import cn.sfrog.system.domain.vo.BizStatusChangeVO;
import cn.sfrog.system.domain.vo.app.AppComboQueryVO;
import cn.sfrog.system.domain.vo.app.AppServiceResultVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;


/**
 * 套餐Service业务层处理
 */
@Service
public class BizComboServiceImpl extends ServiceImpl<BizComboMapper, BizCombo> implements IBizComboService {
    @Resource
    private BizComboMapper bizComboMapper;

    /**
     * 查询套餐
     *
     * @param id 套餐主键
     * @return 套餐
     */
    @Override
    public BizComboVO selectBizComboById(Long id) {
        return bizComboMapper.selectBizComboById(id);
    }

    /**
     * 查询套餐列表
     *
     * @param query 套餐
     * @return 套餐
     */
    @Override
    public List<BizComboVO> selectBizComboList(BizComboQueryVO query) {
        return bizComboMapper.selectBizComboList(query);
    }

    /**
     * 新增套餐
     *
     * @param form 套餐
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean insertBizCombo(BizComboFormVO form) {
        BizCombo bizCombo = new BizCombo();
        BeanUtils.copyProperties(form, bizCombo);
        if(form.getHospitalIds() !=null){
            bizCombo.setHospitalCount(form.getHospitalIds().length);
            bizCombo.setHospitalIds(StringUtils.join(form.getHospitalIds(),","));
        }
        if(form.getServiceIds() !=null){
            bizCombo.setServiceCount(form.getServiceIds().length);
            bizCombo.setServiceIds(StringUtils.join(form.getServiceIds(),","));
        }
        bizCombo.setCreateTime(new Date());
        bizCombo.setCreateBy(SecurityUtils.getUsername());
        bizCombo.setUpdateTime(new Date());
        bizCombo.setUpdateBy(SecurityUtils.getUsername());
        return bizCombo.insert();
    }

    /**
     * 修改套餐
     *
     * @param form 套餐
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean updateBizCombo(BizComboFormVO form) {
        BizCombo bizCombo = new BizCombo();
        BeanUtils.copyProperties(form, bizCombo);
        if(form.getHospitalIds() !=null){
            bizCombo.setHospitalCount(form.getHospitalIds().length);
            bizCombo.setHospitalIds(StringUtils.join(form.getHospitalIds(),","));
        }
        if(form.getServiceIds() !=null){
            bizCombo.setServiceCount(form.getServiceIds().length);
            bizCombo.setServiceIds(StringUtils.join(form.getServiceIds(),","));
        }
        bizCombo.setUpdateTime(new Date());
        bizCombo.setUpdateBy(SecurityUtils.getUsername());
        return bizCombo.updateById();
    }

    /**
     * 批量删除套餐
     *
     * @param ids 需要删除的套餐主键
     * @return 结果
     */
    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean deleteBizComboByIds(Long[] ids) {
            return BizCombo.DAO.delete(Wrappers.lambdaQuery(BizCombo.class).in(BizCombo::getId, (Object[]) ids));
    }

    @Transactional(rollbackFor = Throwable.class)
    @Override
    public boolean changeStatus(BizStatusChangeVO vo) {
        return BizCombo.DAO.update(Wrappers.lambdaUpdate(BizCombo.class).in(BizCombo::getId, (Object[]) vo.getIds()).set(BizCombo::getStatus, vo.getStatus()));
    }

    @Override
    public List<AppServiceResultVO> selectAppComboList(AppComboQueryVO appComboQueryVO) {
        List<AppServiceResultVO> appServiceResultVOS = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(appComboQueryVO.getIds())){
            appServiceResultVOS =  bizComboMapper.selectAppComboList(appComboQueryVO);
        }
        return appServiceResultVOS;
    }

    @Override
    public AppServiceDetailResultVO selectAppComboDetail(Long id) {
        return bizComboMapper.selectAppComboDetail(id);
    }

    @Override
    public AppComboDetailResultVO selectComboById(Long id) {
        AppComboDetailResultVO detailResultVO = new AppComboDetailResultVO();
        BizCombo bizCombo = BizCombo.DAO.selectById(id);
        BeanUtils.copyProperties(bizCombo,detailResultVO);
        if(StringUtils.isNotBlank(bizCombo.getServiceIds())){
            List<AppComboSubServiceVO> serviceVOList = new ArrayList<>();
            String[] split = bizCombo.getServiceIds().split(",");
            List<String> serviceIds = Arrays.asList(split);
            LambdaQueryWrapper<BizService> lambdaQueryWrapper = new LambdaQueryWrapper<BizService>()
                    .in(BizService::getId,serviceIds);
            List<BizService> bizServices = BizService.DAO.selectList(lambdaQueryWrapper);
            if(CollectionUtil.isNotEmpty(bizServices)){
                List<AppComboSubServiceVO> appComboSubServiceVOS = cn.sfrog.common.utils.bean.BeanUtils.convertList2List(bizServices, AppComboSubServiceVO.class);
                detailResultVO.setServiceList(appComboSubServiceVOS);
            }
        }
        LambdaQueryWrapper<BizServiceUser> lambdaQueryWrapper = new LambdaQueryWrapper<BizServiceUser>()
                .eq(BizServiceUser::getProductId,id)
                .eq(BizServiceUser::getUserId,SecurityUtils.getLoginUser().getUser().getUserId())
                .eq(BizServiceUser::getType,Constants.TYPE_COMBO);
        BizServiceUser serviceUser = BizServiceUser.DAO.selectOne(lambdaQueryWrapper);
        if(StringUtils.isNotNull(serviceUser)){
            detailResultVO.setStatus(serviceUser.getStatus());
        }
        return detailResultVO;
    }

    @Override
    public Boolean updateStatusByApp(AppChangeStatusVO vo) {
        LambdaUpdateWrapper<BizServiceUser> updateWrapper = new LambdaUpdateWrapper<BizServiceUser>()
                .eq(BizServiceUser::getType, Constants.TYPE_COMBO)
                .eq(BizServiceUser::getProductId, vo.getId())
                .eq(BizServiceUser::getUserId, SecurityUtils.getLoginUser().getUser().getUserId())
                .set(BizServiceUser::getStatus,vo.getStatus());
        return BizServiceUser.DAO.update(updateWrapper);
    }
}
